package com.zone.vehicle.sdk.module.superapp.cloud;

import com.zone.sdk.common.http.ObservableService;
import com.zone.sdk.common.http.OkHttpManager;
import com.zone.sdk.common.http.base.BaseRequest;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.vehicle.sdk.module.superapp.cloud.request.AppListRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.AppsDetailRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.AppsDownloadQueueRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.AppsDownloadRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.AppsSyncAbilityListRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.AppsUpdateListRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.AppsUploadRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.RecommendListRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.SyncStatusRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.request.UserRequest;

import org.jetbrains.annotations.NotNull;

import io.reactivex.Observable;
import io.reactivex.observers.DisposableObserver;

public class SuperAppCloudServiceManager {

    public static SuperAppCloudServiceManager instance;

    private ObservableService observableService;

    private OkHttpManager okHttpManager;

    ISuperAppCloudService iSuperAppCloudService;

    private SuperAppCloudServiceManager(String appKey, String secret, String url){
        okHttpManager = OkHttpManager.getInstance();
        observableService = ObservableService.getInstance();
        OkHttpManager.HeaderInterceptor headerInterceptor = new OkHttpManager.HeaderInterceptor();
        OkHttpManager.BodyInterceptor bodyInterceptor = new OkHttpManager.BodyInterceptor();
        bodyInterceptor.addParam("appKey", appKey);
        bodyInterceptor.addParam("vin", "vin");
        bodyInterceptor.addParam("vin", "vin");
        iSuperAppCloudService = okHttpManager.build(url, ISuperAppCloudService.class, headerInterceptor, bodyInterceptor);
    }

    public static SuperAppCloudServiceManager getInstance(String appKey, String secret, String url){
        if (instance == null){
            instance = new SuperAppCloudServiceManager(appKey, secret, url);
            return instance;
        }
        return instance;
    }

    private void innerExecute(Observable observable, ResultCallBack<Void> callback){
        observableService.execute(observable, new DisposableObserver<Void>(){

            @Override
            public void onNext(@NotNull Void o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }


    public void appList(AppListRequest request, ResultCallBack<Void> callback){

        observableService.execute(iSuperAppCloudService.appList(request), new DisposableObserver<Void>(){

            @Override
            public void onNext(@NotNull Void o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 应用状态同步
     * @param request
     * @param callback
     */
    public void syncStatus(SyncStatusRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.syncStatus(request), callback);
    }

    /**
     * 应用更新列表查询
     * @param request
     * @param callback
     */
    public void appsUpdateList(AppsUpdateListRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.appsUpdateList(request), callback);
    }

    /**
     * 获取活动内容
     * @param request
     * @param callback
     */
    public void recommendList(RecommendListRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.recommendList(request), callback);
    }

    /**
     * 应用详情
     * @param request
     * @param callback
     */
    public void appsDetail(AppsDetailRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.appsDetail(request), callback);
    }

    /**
     * 用户信息查询
     * @param request
     * @param callback
     */
    public void user(UserRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.user(request), callback);
    }

    /**
     * 应用更新列表查询
     * @param request
     * @param callback
     */
    public void appsDownload(AppsDownloadRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.appsDownload(request), callback);
    }

    /**
     * 开发者通过此接口将小程序的白名单同步到
     * appStore
     * @param request
     * @param callback
     */
    public void appsSyncAbilityList(AppsSyncAbilityListRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.appsSyncAbilityList(request), callback);
    }

    /**
     * 车机端同步下载队列应用并由云端推送到手机端
     * @param request
     * @param callback
     */
    public void appsDownloadQueue(AppsDownloadQueueRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.appsDownloadQueue(request), callback);
    }

    /**
     * 文件上传（图片）
     * @param request
     * @param callback
     */
    public void appsUpload(AppsUploadRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.appsUpload(request), callback);
    }

    /**
     * 车机端同步下载队列应用并由云端推送到手机端
     * @param request
     * @param callback
     */
    public void appSearch(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.indexSearch(request), callback);
    }

    /**
     * 下载用户对app进行评价及安利
     * @param request
     * @param callback
     */
    public void commentEditComment(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentEditComment(request), callback);
    }

    /**
     * 下载用户对app下的用户评论进行回复
     * @param request
     * @param callback
     */
    public void commentEditReply(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentEditReply(request), callback);
    }

    /**
     * 对用户的评论进行点赞
     * @param request
     * @param callback
     */
    public void commentCommentPraise(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentCommentPraise(request), callback);
    }

    /**
     * 用户评论审核不通过，可对当前评论进行修改
     * @param request
     * @param callback
     */
    public void commentInitCommentDetail(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentInitCommentDetail(request), callback);
    }

    /**
     * 用户评论审核不通过，可对当前评论进行修改
     * @param request
     * @param callback
     */
    public void commentUpdateComment(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentUpdateComment(request), callback);
    }

    /**
     * 显示app总体评分和所有用户评价列表
     * @param request
     * @param callback
     */
    public void commentCommentScore(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentCommentScore(request), callback);
    }

    /**
     * 显示app总体评分和所有用户评价列表
     * @param request
     * @param callback
     */
    public void commentCommentList(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentCommentList(request), callback);
    }

    /**
     * 显示app总体评分和所有用户评价列表
     * @param request
     * @param callback
     */
    public void commentCommentDetail(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentCommentDetail(request), callback);
    }

    /**
     * 分页显示安利墙列表
     * @param request
     * @param callback
     */
    public void commentInitAmwayWallList(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentInitAmwayWallList(request), callback);
    }

    /**
     * 用户查看自己评论过的信息列表
     * @param request
     * @param callback
     */
    public void commentUserCommentList(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentUserCommentList(request), callback);
    }

    /**
     * 用户删除自己的评论
     * @param request
     * @param callback
     */
    public void commentDelUserComment(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentDelUserComment(request), callback);
    }

    /**
     * 查询应用下评论数，不包含回复和回复回复
     * @param request
     * @param callback
     */
    public void commentSearchCommentCount(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.commentSearchCommentCount(request), callback);
    }

    /**
     * 用户提交意见反馈
     * @param request
     * @param callback
     */
    public void feedbackFeedbackCommit(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.feedbackFeedbackCommit(request), callback);
    }

    /**
     * 车机端同步下载队列应用并由云端推送到手机端
     * /user/captchacert/exchange/v1
     * @param request
     * @param callback
     */
    public void captchacertExchange(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.captchacertExchange(request), callback);
    }

    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     * @param request
     * @param callback
     */
    public void loginPhoneCode(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.loginPhoneCode(request), callback);
    }

    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     * 与获取验证码接口相同，请求参数做区分 loginPhoneCode
     * @param request
     * @param callback
     */
    public void loginPhoneCodeCheck(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.loginPhoneCode(request), callback);
    }

    /**
     * 手机号+密码登录流程中，调用此接口提交登录请求，并获取登录信息
     * @param request
     * @param callback
     */
    public void loginPhonePwd(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.loginPhonePwd(request), callback);
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口设置密码。
     * @param request
     * @param callback
     */
    public void userPwdModify(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.userPwdModify(request), callback);
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口获取个人基本信息
     * @param request
     * @param callback
     */
    public void queryUserInfo(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.queryUserInfo(request), callback);
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口修改个人基本信息。
     * @param request
     * @param callback
     */
    public void updateUserInfo(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.updateUserInfo(request), callback);
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口退出当前账号
     * @param request
     * @param callback
     */
    public void logout(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.logout(request), callback);
    }

    /**
     * 用户Access Token 过期状态下，调用此接口以获取新的Token。
     * @param request
     * @param callback
     */
    public void refreshToken(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.refreshToken(request), callback);
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口上传文件（移动端用于上传自定义头像）。
     * @param request
     * @param callback
     */
    public void userUpload(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.userUpload(request), callback);
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口查询当前账户在所有设备上的登录状态
     * @param request
     * @param callback
     */
    public void loginStatus(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.loginStatus(request), callback);
    }

    /**
     * 车机端调用此接口生成登录二维码，返回的是QR Code URL
     * @param request
     * @param callback
     */
    public void qrLoginCode(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.qrLoginCode(request), callback);
    }

    /**
     * 车机端通过此接口轮询登录状态。
     * @param request
     * @param callback
     */
    public void qrLoginCodeStatus(BaseRequest request, ResultCallBack<Void> callback){
        innerExecute(iSuperAppCloudService.qrLoginCodeStatus(request), callback);
    }
}
